/********************************** (C) COPYRIGHT *******************************
 * File Name          : app_usb.c
 * Author             : L&E
 * Version            : V1.1
 * Date               : 2024/04/10
 * Description        : Simulate XBOX Controler
 *******************************************************************************/

/*********************************************************************
 * INCLUDES
 */
#include "CONFIG.h"
#include "gattprofile.h"
#include "stdint.h"
#include "central.h"
#include "RingMem.h"
#include "app_usb.h"

#include "CH59x_usbdev.h"
#include "CH592SFR.h"

/*********************************************************************
 * MACROS
 */
#define DevEP0SIZE  0x08	//端点0大小

/*********************************************************************
 * CONSTANTS
 */

/*字符串描述符*/
const uint8_t MyLangDescr[] = { 0x04, 0x03, 0x09, 0x04 };//0.语言描述符(0x0409为美式英语)
const uint8_t MyManuInfo[] = {//1.厂家信息"©Microsoft Corporation"
	46,3,194,169,77,0,105,0,99,0,114,0,111,0,115,0,111,0,102,0,116,0,32,0,
	67,0,111,0,114,0,112,0,111,0,114,0,97,0,116,0,105,0,111,0,110,0,
};
const uint8_t MyProdInfo[] = {22,3,67,0,111,0,110,0,116,0,114,0,111,0,108,0,108,0,101,0,114,0,};//2.产品信息"Controller"
//const uint8_t MySrNumInfo[] = {16,3,'0',0,'8',0,'F',0,'E',0,'C',0,'9',0,'3',0,};//3.序列号信息"08FEC93"
const uint8_t MySrNumInfo[] = {16,3,'0',0,'8',0,'F',0,'E',0,'5',0,'9',0,'1',0,};//3.序列号信息"08FE591"
const uint8_t MySafeInfo[] = {//4.安全信息"Xbox Security Method 3, Version 1.00, © 2005 Microsoft Corporation. All rights reserved."
	178,3,88,0,98,0,111,0,120,0,32,0,83,0,101,0,99,0,117,0,114,0,105,0,116,0,121,0,32,0,77,0,101,0,116,0,
	104,0,111,0,100,0,32,0,51,0,44,0,32,0,86,0,101,0,114,0,115,0,105,0,111,0,110,0,32,0,49,0,46,0,48,0,
	48,0,44,0,32,0,194,169,32,0,50,0,48,0,48,0,53,0,32,0,77,0,105,0,99,0,114,0,111,0,115,0,111,0,102,0,
	116,0,32,0,67,0,111,0,114,0,112,0,111,0,114,0,97,0,116,0,105,0,111,0,110,0,46,0,32,0,65,0,108,0,108,
	0,32,0,114,0,105,0,103,0,104,0,116,0,115,0,32,0,114,0,101,0,115,0,101,0,114,0,118,0,101,0,100,0,46,0,
};//首字节为总长度，之后固定为3以表示字符串描述符，之后为Unicode字符串

/*设备描述符*/
const uint8_t MyDevDescr[] = {//设备描述符
	0x12, // bLength
	0x01, // bDescriptorType
	0x00, 0x02, // bcdUSB (2.0)
	0xFF, // bDeviceClass
	0xFF, // bDeviceSubClass
	0xFF, // bDeviceProtocol
	0x08, // bMaxPacketSize0
	0x5E, 0x04, // idEVendor (Microsoft Corp.)
	0x8E, 0x02, // idProduct (Xbox360 Controller)
	0x14, 0x01, // bcdDevice
	0x01, // iManufacturer
	0x02, // iProduct
	0x03, // iSerialNumber
	0x01, // bNumConfigurations
};
/*配置描述符*/
const uint8_t MyCfgDescr[] = {//配置描述符
	// Configuration Descriptor
	0x09, // bLength
	0x02, // bDescriptorType (CONFIGURATION)
	/*49,0,*/0x99, 0x00, // wTotalLength (153)
	/*1,*/0x04, // bNumInterfaces
	0x01, // bConfigurationValue
	0x00, // iConfiguration
	0xA0, // bmAttributes
	0xFA, // bMaxPower
	/* ---------------------------------------------------- */
	// Interface 0: Control Data
	0x09, // bLength
	0x04, // bDescriptorType (INTERFACE)
	0x00, // bInterfaceNumber
	0x00, // bAlternateSetting
	0x02, // bNumEndpoints
	0xFF, // bInterfaceClass
	0x5D, // bInterfaceSubClass
	0x01, // bInterfaceProtocol
	0x00, // iInterface
	// Unknown Descriptor (If0)
	0x11, // bLength
	0x21, // bDescriptorType
	0x00, 0x01, 0x01, 0x25, // ???
	0x81, // bEndpointAddress (IN, 1)
	0x14, // bMaxDataSize
	0x00, 0x00, 0x00, 0x00, 0x13, // ???
	0x01, // bEndpointAddress (OUT, 1)
	0x08, // bMaxDataSize
	0x00, 0x00, // ???
	// Endpoint 1: Control Surface Send
	0x07, // bLength
	0x05, // bDescriptorType (ENDPOINT)
	0x81, // bEndpointAddress (IN, 1)
	0x03, // bmAttributes
	0x20, 0x00, // wMaxPacketSize
	0x04, // bInterval
	// Endpoint 1: Control Surface Receive
	0x07, // bLength
	0x05, // bDescriptorType (ENDPOINT)
	0x01, // bEndpointAddress (OUT, 1)
	0x03, // bmAttributes
	0x20, 0x00, // wMaxPacketSize
	0x08, // bInterval
	/* ---------------------------------------------------- */
	// Interface 1: Headset (and Expansion Port?)
	0x09, // bLength
	0x04, // bDescriptorType (INTERFACE)
	0x01, // bInterfaceNumber
	0x00, // bAlternateSetting
	0x04, // bNumEndpoints
	0xFF, // bInterfaceClass
	0x5D, // bInterfaceSubClass
	0x03, // bInterfaceProtocol
	0x00, // iInterface
	// Unknown Descriptor (If1)
	0x1B, // bLength
	0x21, // bDescriptorType
	0x00, 0x01, 0x01, 0x01, // ???
	0x82, // bEndpointAddress (IN, 2)
	0x40, // bMaxDataSize
	0x01, // ???
	0x02, // bEndpointAddress (OUT, 2)
	0x20, // bMaxDataSize
	0x16, // ???
	0x83, // bEndpointAddress (IN, 3)
	0x00, // bMaxDataSize
	0x00, 0x00, 0x00, 0x00, 0x00, 0x16, // ???
	0x03, // bEndpointAddress (OUT, 3)
	0x00, // bMaxDataSize
	0x00, 0x00, 0x00, 0x00, 0x00, // ???
	// Endpoint 2: Microphone Data Send
	0x07, // bLength
	0x05, // bDescriptorType (ENDPOINT)
	0x82, // bEndpointAddress (IN, 2)
	0x03, // bmAttributes
	0x20, 0x00, // wMaxPacketSize
	0x02, // bInterval
	// Endpoint 2: Headset Audio Receive
	0x07, // bLength
	0x05, // bDescriptorType (ENDPOINT)
	0x02, // bEndpointAddress (OUT, 2)
	0x03, // bmAttributes
	0x20, 0x00, // wMaxPacketSize
	0x04, // bInterval
	// Endpoint 3: Unknown, Send
	0x07, // bLength
	0x05, // bDescriptorType (ENDPOINT)
	0x83, // bEndpointAddress (IN, 3)
	0x03, // bmAttributes
	0x20, 0x00, // wMaxPacketSize
	0x40, // bInterval
	// Endpoint 3: Unknown, Receive
	0x07, // bLength
	0x05, // bDescriptorType (ENDPOINT)
	0x03, // bEndpointAddress (OUT, 3)
	0x03, // bmAttributes
	0x20, 0x00, // wMaxPacketSize
	0x10, // bInterval
	/* ---------------------------------------------------- */
	// Interface 2: Unknown
	0x09, // bLength
	0x04, // bDescriptorType (INTERFACE)
	0x02, // bInterfaceNumber
	0x00, // bAlternateSetting
	0x01, // bNumEndpoints
	0xFF, // bInterfaceClass
	0x5D, // bInterfaceSubClass
	0x02, // bInterfaceProtocol
	0x00, // iInterface
	// Unknown Descriptor (If2)
	0x09, // bLength
	0x21, // bDescriptorType
	0x00, 0x01, 0x01, 0x22, // ???
	0x84, // bEndpointAddress (IN, 4)
	0x07, // bMaxDataSize
	0x00, // ???
	// Endpoint 4: Unknown, Send
	0x07, // bLength
	0x05, // bDescriptorType (ENDPOINT)
	0x84, // bEndpointAddress (IN, 4)
	0x03, // bmAttributes
	0x20, 0x00, // wMaxPacketSize
	0x10, // bInterval
	/* ---------------------------------------------------- */
	// Interface 3: Security Method
	0x09, // bLength
	0x04, // bDescriptorType (INTERFACE)
	0x03, // bInterfaceNumber
	0x00, // bAlternateSetting
	0x00, // bNumEndpoints
	0xFF, // bInterfaceClass
	0xFD, // bInterfaceSubClass
	0x13, // bInterfaceProtocol
	0x04, // iInterface
	// Unknown Descriptor (If3)
	0x06, // bLength
	0x41, // bDescriptorType
	0x00, 0x01, 0x01, 0x03, // ???
};

/*********************************************************************
 * LOCAL VARIABLES
 */
const uint8_t *pDescr;
uint8_t	DevConfig, Ready;
uint8_t	SetupReqCode;
UINT16	SetupReqLen;

/******** 用户自定义分配端点RAM ****************************************/
__attribute__((aligned(4)))  uint8_t EP0_Databuf[64 + 64 + 64];    //ep0(64)+ep4_out(64)+ep4_in(64)
__attribute__((aligned(4)))  uint8_t EP1_Databuf[64 + 64];    //ep1_out(64)+ep1_in(64)
__attribute__((aligned(4)))  uint8_t EP2_Databuf[64 + 64];    //ep2_out(64)+ep2_in(64)
__attribute__((aligned(4)))  uint8_t EP3_Databuf[64 + 64];    //ep3_out(64)+ep3_in(64)

/*********************************************************************
 * PUBLIC FUNCTIONS
 */

/*********************************************************************
 * @fn      app_usb_init
 *
 * @brief   初始化usb
 *
 * @return  none
 */
void app_usb_init()
{
    pEP0_RAM_Addr = EP0_Databuf;
    pEP1_RAM_Addr = EP1_Databuf;
    pEP2_RAM_Addr = EP2_Databuf;
    pEP3_RAM_Addr = EP3_Databuf;

    USB_DeviceInit();
    PFIC_EnableIRQ( USB_IRQn );
}

/*********************************************************************
 * @fn      DevEP1_Send
 *
 * @brief   初始化usb
 *
 * @return  none
 */
uint8_t DevEP1_Send(uint8_t *buf, uint8_t len)
{
	if((R8_UEP1_CTRL & MASK_UEP_T_RES) != UEP_T_RES_ACK)
    {
		memcpy(pEP1_IN_DataBuf, buf, len);
        DevEP1_IN_Deal(len);
        return SUCCESS;
    }
	return FAILURE;
}

/*********************************************************************
 * @fn      USBSendData
 *
 * @brief   发送数据给主机
 *
 * @return  none
 */
uint8_t USBSendData(void)
{
    if((R8_UEP2_CTRL & MASK_UEP_T_RES) == UEP_T_RES_ACK){
        return FAILURE;
    }
    if(RingMemBLE.CurrentLen > 32){
        RingMemRead(&RingMemBLE, pEP2_IN_DataBuf, 32);
        DevEP2_IN_Deal(32);
    }
    else{
        uint8_t len = RingMemBLE.CurrentLen;
        RingMemRead(&RingMemBLE, pEP2_IN_DataBuf, len);
        DevEP2_IN_Deal(len);
    }
    return SUCCESS;
}

/*********************************************************************
 * @fn      DevEP1_OUT_Deal
 *
 * @brief   端点1数据处理
 *
 * @return  none
 */
void DevEP1_OUT_Deal( uint8_t l )
{ /* 用户可自定义 */
}

/*********************************************************************
 * @fn      DevEP2_OUT_Deal
 *
 * @brief   端点2数据处理
 *
 * @return  none
 */
void DevEP2_OUT_Deal( uint8_t l )
{ /* 用户可自定义 */
//    if(RingMemWrite(&RingMemUSB, pEP2_OUT_DataBuf, l) != SUCCESS)
//    {
//        PRINT("RingMemBLE ERR \n");
//    }
    //tmos_start_task(Peripheral_TaskID, SBP_PROCESS_USBDATA_EVT, 32);
}

/*********************************************************************
 * @fn      DevEP3_OUT_Deal
 *
 * @brief   端点3数据处理
 *
 * @return  none
 */
void DevEP3_OUT_Deal( uint8_t l )
{ /* 用户可自定义 */
}

/*********************************************************************
 * @fn      DevEP4_OUT_Deal
 *
 * @brief   端点4数据处理
 *
 * @return  none
 */
void DevEP4_OUT_Deal( uint8_t l )
{ /* 用户可自定义 */
}

/*********************************************************************
 * @fn      USB_DevTransProcess
 *
 * @brief   USB 传输处理函数
 *
 * @return  none
 */
void USB_DevTransProcess( void )
{
  uint8_t chtype, len, intflag, errflag = 0;

  intflag = R8_USB_INT_FG;
  if ( intflag & RB_UIF_TRANSFER )
  {
    if ( ( R8_USB_INT_ST & MASK_UIS_TOKEN ) != MASK_UIS_TOKEN )		// 非空闲
    {
      switch ( R8_USB_INT_ST & ( MASK_UIS_TOKEN | MASK_UIS_ENDP ) )
      // 分析操作令牌和端点号
      {
        case UIS_TOKEN_IN :	{		//端点0 IN
          switch ( SetupReqCode )
          {
            case USB_GET_DESCRIPTOR :
              len = SetupReqLen >= DevEP0SIZE ? DevEP0SIZE : SetupReqLen;	// 本次传输长度
              memcpy( pEP0_DataBuf, pDescr, len ); /* 加载上传数据 */
              SetupReqLen -= len;
              pDescr += len;
              R8_UEP0_T_LEN = len;
              R8_UEP0_CTRL ^= RB_UEP_T_TOG;						// 翻转
              break;
            case USB_SET_ADDRESS :
              R8_USB_DEV_AD = ( R8_USB_DEV_AD & RB_UDA_GP_BIT ) | SetupReqLen;
              R8_UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
              break;
            default :
              R8_UEP0_T_LEN = 0;								// 状态阶段完成中断或者是强制上传0长度数据包结束控制传输
              R8_UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
              break;
          }
        }break;

        case UIS_TOKEN_OUT : {		//端点0 OUT
          len = R8_USB_RX_LEN;
          //R8_UEP0_CTRL ^= RB_UEP_R_TOG; // 翻转(增补)
        }break;

        case UIS_TOKEN_OUT | 1 : {	//端点1 OUT
          if ( R8_USB_INT_ST & RB_UIS_TOG_OK ){// 不同步的数据包将丢弃
            len = R8_USB_RX_LEN;
            DevEP1_OUT_Deal( len );
          }
        }break;
        case UIS_TOKEN_IN | 1 :	{	//端点1 IN
          R8_UEP1_CTRL = ( R8_UEP1_CTRL & ~MASK_UEP_T_RES ) | UEP_T_RES_NAK;
        }break;

        case UIS_TOKEN_OUT | 2 : {	//端点2 OUT
          if ( R8_USB_INT_ST & RB_UIS_TOG_OK ){// 不同步的数据包将丢弃
            len = R8_USB_RX_LEN;
            DevEP2_OUT_Deal( len );
          }
        }break;
        case UIS_TOKEN_IN | 2 :	{	//端点2 IN
          R8_UEP2_CTRL = ( R8_UEP2_CTRL & ~MASK_UEP_T_RES ) | UEP_T_RES_NAK;
        }break;

        case UIS_TOKEN_OUT | 3 : {	//端点3 OUT
          if ( R8_USB_INT_ST & RB_UIS_TOG_OK ){// 不同步的数据包将丢弃
            len = R8_USB_RX_LEN;
            DevEP3_OUT_Deal( len );
          }
        }break;
        case UIS_TOKEN_IN | 3 :		//端点3 IN
          R8_UEP3_CTRL = ( R8_UEP3_CTRL & ~MASK_UEP_T_RES ) | UEP_T_RES_NAK;
          break;

        case UIS_TOKEN_OUT | 4 : {	//端点4 OUT
          if ( R8_USB_INT_ST & RB_UIS_TOG_OK ){
            R8_UEP4_CTRL ^= RB_UEP_R_TOG;
            len = R8_USB_RX_LEN;
            DevEP4_OUT_Deal( len );
          }
        }break;
        case UIS_TOKEN_IN | 4 : {	//端点4 IN
          R8_UEP4_CTRL ^= RB_UEP_T_TOG;
          R8_UEP4_CTRL = ( R8_UEP4_CTRL & ~MASK_UEP_T_RES ) | UEP_T_RES_NAK;
        }break;

        default : break;			//其他
      }
      R8_USB_INT_FG = RB_UIF_TRANSFER;
    }
    if ( R8_USB_INT_ST & RB_UIS_SETUP_ACT )					// Setup包处理
    {
      R8_UEP0_CTRL = RB_UEP_R_TOG | RB_UEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_NAK;
      SetupReqLen = pSetupReqPak->wLength;
      SetupReqCode = pSetupReqPak->bRequest;
      chtype = pSetupReqPak->bRequestType;

      len = 0;
      errflag = 0;
      if ( ( pSetupReqPak->bRequestType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD )
      {
        switch( SetupReqCode ){
			case 0x01:break;//GetReport
			case 0x02:break;//GetIdle
			case 0x03:break;//GetProtocol
			case 0x09:break;//SetReport
			case 0x0A:break;//SetIdle
			case 0x0B:break;//SetProtocol
			default:			//不支持的命令或者出错
				errflag = 0xFF;
				break;
		}
        errflag = 0xff;//这里要直接置0xFF否则会使电脑USB不正常响应
      }
      else /* 标准请求 */
      {
        switch ( SetupReqCode )		//请求码
        {
          case USB_GET_DESCRIPTOR :		//获取描述符
          {
            switch ( ( ( pSetupReqPak->wValue ) >> 8 ) )
            {
              case USB_DESCR_TYP_DEVICE : {		//设备描述符
                pDescr = MyDevDescr;
                len = sizeof(MyDevDescr);
              }break;

              case USB_DESCR_TYP_CONFIG : {		//配置描述符
                pDescr = MyCfgDescr;
                len = sizeof(MyCfgDescr);
              }break;

              case USB_DESCR_TYP_STRING :		//字符串描述符
              {
                switch ( ( pSetupReqPak->wValue ) & 0xff )
                {
				  case 0 :							//语言信息
                    pDescr = MyLangDescr;
                    len = sizeof(MyLangDescr);
                    break;
                  case 1 :							//厂商信息
                    pDescr = MyManuInfo;
                    len = sizeof(MyManuInfo);
                    break;
                  case 2 :							//产品信息
                    pDescr = MyProdInfo;
                    len = sizeof(MyProdInfo);
                    break;
                  case 3 :							//序列号信息
                    pDescr = MySrNumInfo;
                    len = sizeof(MySrNumInfo);
                    break;
                  case 4 :							//安全信息
                    pDescr = MySafeInfo;
                    len = sizeof(MySafeInfo);
                    break;
                  default :							// 不支持的字符串描述符
                    errflag = 0xFF;
                    break;
                }
              }
                break;

              default :
                errflag = 0xff;
                break;
            }
            if ( SetupReqLen > len ) SetupReqLen = len;      //实际需上传总长度
            len = ( SetupReqLen >= DevEP0SIZE ) ? DevEP0SIZE : SetupReqLen;
            memcpy( pEP0_DataBuf, pDescr, len );
            pDescr += len;
          }
            break;

          case USB_SET_ADDRESS :
            SetupReqLen = ( pSetupReqPak->wValue ) & 0xff;
            break;

          case USB_GET_CONFIGURATION :
            pEP0_DataBuf[0] = DevConfig;
            if ( SetupReqLen > 1 ) SetupReqLen = 1;
            break;

          case USB_SET_CONFIGURATION :
            DevConfig = ( pSetupReqPak->wValue ) & 0xff;
            break;

          case USB_CLEAR_FEATURE :
          {
            if ( ( pSetupReqPak->bRequestType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_ENDP )    // 端点
            {
              switch ( ( pSetupReqPak->wIndex ) & 0xff )
              {
                case 0x81 :
                  R8_UEP1_CTRL = ( R8_UEP1_CTRL & ~( RB_UEP_T_TOG | MASK_UEP_T_RES ) ) | UEP_T_RES_NAK;
                  break;
                case 0x01 :
                  R8_UEP1_CTRL = ( R8_UEP1_CTRL & ~( RB_UEP_R_TOG | MASK_UEP_R_RES ) ) | UEP_R_RES_ACK;
                  break;
                case 0x82 :
                  R8_UEP2_CTRL = ( R8_UEP2_CTRL & ~( RB_UEP_T_TOG | MASK_UEP_T_RES ) ) | UEP_T_RES_NAK;
                  break;
                case 0x02 :
                  R8_UEP2_CTRL = ( R8_UEP2_CTRL & ~( RB_UEP_R_TOG | MASK_UEP_R_RES ) ) | UEP_R_RES_ACK;
                  break;
                case 0x83 :
                  R8_UEP3_CTRL = ( R8_UEP3_CTRL & ~( RB_UEP_T_TOG | MASK_UEP_T_RES ) ) | UEP_T_RES_NAK;
                  break;
                case 0x03 :
                  R8_UEP3_CTRL = ( R8_UEP3_CTRL & ~( RB_UEP_R_TOG | MASK_UEP_R_RES ) ) | UEP_R_RES_ACK;
                  break;
                case 0x84 :
                  R8_UEP4_CTRL = ( R8_UEP4_CTRL & ~( RB_UEP_T_TOG | MASK_UEP_T_RES ) ) | UEP_T_RES_NAK;
                  break;
                case 0x04 :
                  R8_UEP4_CTRL = ( R8_UEP4_CTRL & ~( RB_UEP_R_TOG | MASK_UEP_R_RES ) ) | UEP_R_RES_ACK;
                  break;
                default :
                  errflag = 0xFF;					// 不支持的端点
                  break;
              }
            }
            else
              errflag = 0xFF;
          }
            break;
          case USB_GET_INTERFACE :
            pEP0_DataBuf[0] = 0x00;
            if ( SetupReqLen > 1 ) SetupReqLen = 1;
            break;

          case USB_GET_STATUS :
            pEP0_DataBuf[0] = 0x00;
            pEP0_DataBuf[1] = 0x00;
            if ( SetupReqLen > 2 ) SetupReqLen = 2;
            break;

          default :
            errflag = 0xff;
            break;
        }
      }
      if ( errflag == 0xff )        // 错误或不支持
      {
//                  SetupReqCode = 0xFF;
        R8_UEP0_CTRL = RB_UEP_R_TOG | RB_UEP_T_TOG | UEP_R_RES_STALL | UEP_T_RES_STALL;    // STALL
      }
      else
      {
        if ( chtype & 0x80 )     // 上传
        {
          len = ( SetupReqLen > DevEP0SIZE ) ? DevEP0SIZE : SetupReqLen;
          SetupReqLen -= len;
        }
        else len = 0;        // 下传
        R8_UEP0_T_LEN = len;
        R8_UEP0_CTRL = RB_UEP_R_TOG | RB_UEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK;    // 默认数据包是DATA1
      }

      R8_USB_INT_FG = RB_UIF_TRANSFER;
    }
  }
  else if ( intflag & RB_UIF_BUS_RST )
  {
    R8_USB_DEV_AD = 0;
    R8_UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
    R8_UEP1_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK | RB_UEP_AUTO_TOG;
    R8_UEP2_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK | RB_UEP_AUTO_TOG;
    R8_UEP3_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK | RB_UEP_AUTO_TOG;
    R8_USB_INT_FG = RB_UIF_BUS_RST;
  }
  else if ( intflag & RB_UIF_SUSPEND )
  {
    if( R8_USB_MIS_ST & RB_UMS_SUSPEND ){}	// 挂起
    else{}									// 唤醒
    R8_USB_INT_FG = RB_UIF_SUSPEND;
  }
  else{
    R8_USB_INT_FG = intflag;
  }
}

/*********************************************************************
 * @fn      USB_IRQHandler
 *
 * @brief   USB中断函数
 *
 * @return  none
 */
__attribute__((interrupt("WCH-Interrupt-fast")))
__attribute__((section(".highcode")))
void USB_IRQHandler( void ) /* USB中断服务程序,使用寄存器组1 */
{
	USB_DevTransProcess();
}


/*********************************************************************
*********************************************************************/
